// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

#include "NumberMunchersGame.h"

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")


 // include the DirectX Input Library files
#pragma comment (lib, "dinput.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")


// function prototypes
void WinMain_Render(void);    // renders a single frame
void WinMain_Update(double frameTime);

NumberMunchersGame nmGame; 


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          L"WindowClass",
                          L"Number Munchers 3D",
                          WS_OVERLAPPEDWINDOW,
                          300, 300,
                          800, 600,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize The engine
	nmGame.Initialise(hInstance, hWnd);
//	TestInit();

    // enter the main loop:

    MSG msg;

	//DWORD frameStartTime = 0;
	//DWORD frameEndTime = 0;	
	double frameElapsedTime = 0;


    while(TRUE)
    {      

		LARGE_INTEGER start, stop, freq;

		QueryPerformanceFrequency(&freq);

		//frameStartTime = GetTickCount();
		QueryPerformanceCounter(&start);


        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
		
		WinMain_Update(frameElapsedTime);
        WinMain_Render();

		//frameEndTime = GetTickCount();
		QueryPerformanceCounter(&stop);

		//frameElapsedTime = (frameEndTime - frameStartTime); //frametime in seconds
		//frameElapsedTime = frameElapsedTime / 1000;
		frameElapsedTime = (stop.QuadPart - start.QuadPart);
		frameElapsedTime = frameElapsedTime / freq.QuadPart;

    }

    // clean up DirectX and COM
	nmGame.Clean();

    return (int)msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}



// this is the function used to render a single frame
void WinMain_Render()
{
    nmGame.Draw();
}

void WinMain_Update(double frameTime)
{
	nmGame.Update(frameTime);
}